package cn.ilikes.tools.untils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import cn.ilikes.tools.hbase.jpa.clients.nodes.Node;
import cn.ilikes.tools.hbase.jpa.metadatas.elements.EntityFields;

public class ObjectUtils {

	public static boolean isRelation(EntityFields entityFields) {
		return (entityFields.getjPAField() != null && entityFields.getjPAField().isRelation());
	}

	public static List<Object> getEntitys(List<Node> nodes) {
		List<Object> entitys = new ArrayList<Object>();
		for (Node node : nodes) {
			entitys.add(node.getEntity());
		}
		return entitys;
	}

	public static Object instance(Class<?> clazz) throws ObjectUtilsException {
		try {
			Object entity = clazz.newInstance();
			return entity;
		} catch (InstantiationException e) {
			throw new ObjectUtilsException(e);
		} catch (IllegalAccessException e) {
			throw new ObjectUtilsException(e);
		}
	}

	public static void setFieldValue(Field field, Object entity, Object value) throws ObjectUtilsException {
		if (!field.isAccessible())
			field.setAccessible(true);
		try {
			field.set(entity, value);
		} catch (IllegalArgumentException e) {
			throw new ObjectUtilsException(e);
		} catch (IllegalAccessException e) {
			throw new ObjectUtilsException(e);
		}
	}

}
